# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

# head=ListNode(3)
# head.next=ListNode(2)
# head.next.next=ListNode(0)
# head.next.next.next=ListNode(-4)
head=ListNode(1)
head.next=ListNode(2)
# head.next.next=head
def hasCycle(head):
    if head==None:
        return False
    elif head.next==None:
        return False
    #使用快慢指针来求解环的存在
    headProcess=head
    headNextProcess=head.next
    while headProcess and headNextProcess:
        if headProcess==headNextProcess:
            return True
        if headNextProcess.next==None:
            return False
        headProcess=headProcess.next
        headNextProcess=headNextProcess.next.next
    return False


print(hasCycle(head))

